package com.petdog.module.report.controller;

import com.petdog.common.dto.PageResult;
import com.petdog.common.dto.ApiResponse;
import com.petdog.module.report.dto.ReportDto;
import com.petdog.module.report.dto.ReportProcessRequest;
import com.petdog.module.report.dto.ReportRequest;
import com.petdog.module.report.service.ReportService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 举报控制器
 */
@RestController
@RequestMapping("/api/report")
@Validated
public class ReportController {

    @Resource
    private ReportService reportService;

    /**
     * 提交举报
     */
    @PostMapping
    public ApiResponse<Long> submitReport(@Valid @RequestBody ReportRequest request, 
                                        @RequestHeader("userId") Long reporterId) {
        Long reportId = reportService.submitReport(request, reporterId);
        return ApiResponse.success(reportId, "举报提交成功");
    }

    /**
     * 获取举报详情
     */
    @GetMapping("/{reportId}")
    public ApiResponse<ReportDto> getReportById(@PathVariable Long reportId, 
                                              @RequestHeader("userId") Long operatorId) {
        // 这里可以添加权限校验，确保只有管理员或相关人员才能查看
        ReportDto report = reportService.getReportById(reportId);
        return ApiResponse.success(report);
    }

    /**
     * 分页查询举报列表
     */
    @GetMapping("/list")
    public ApiResponse<PageResult<ReportDto>> getReportList(@RequestParam(defaultValue = "1") Integer pageNum, 
                                                         @RequestParam(defaultValue = "10") Integer pageSize, 
                                                         @RequestParam(required = false) Long reporterId, 
                                                         @RequestParam(required = false) Integer contentType, 
                                                         @RequestParam(required = false) Long contentId, 
                                                         @RequestParam(required = false) Integer reportType, 
                                                         @RequestParam(required = false) Integer status, 
                                                         @RequestHeader("userId") Long operatorId) {
        // 这里可以添加权限校验，确保只有管理员或相关人员才能查询
        PageResult<ReportDto> result = reportService.getReportList(pageNum, pageSize, reporterId, contentType, contentId, reportType, status);
        return ApiResponse.success(result);
    }

    /**
     * 处理举报
     */
    @PutMapping
    public ApiResponse<Boolean> processReport(@Valid @RequestBody ReportProcessRequest request, 
                                            @RequestHeader("userId") Long processorId) {
        // 这里可以添加权限校验，确保只有管理员才能处理
        boolean result = reportService.processReport(request, processorId);
        return ApiResponse.success(result, result ? "举报处理成功" : "举报处理失败");
    }

    /**
     * 统计举报数量
     */
    @GetMapping("/count")
    public ApiResponse<Long> countReports(@RequestParam(required = false) Integer status, 
                                        @RequestHeader("userId") Long operatorId) {
        // 这里可以添加权限校验，确保只有管理员才能统计
        long count = reportService.countReports(status);
        return ApiResponse.success(count);
    }

    /**
     * 获取指定内容的举报数量
     */
    @GetMapping("/content/count")
    public ApiResponse<Integer> getReportCountByContent(@RequestParam Integer contentType, 
                                                     @RequestParam Long contentId, 
                                                     @RequestParam(required = false) Integer status) {
        int count = reportService.getReportCountByContent(contentType, contentId, status);
        return ApiResponse.success(count);
    }
}